﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Core
{
    public class Helper
    {
        public  static List<EntityTypeProperty> ResolveSqlQuery(EntityType entityType, DataRow row)
        {
            // This class will resolve a T-SQL Statement Result into a list of entitytypes
            List<EntityTypeProperty> etpList = new List<EntityTypeProperty>();

            foreach (DataColumn column in row.Table.Columns)
            {
                EntityTypeProperty etp = new EntityTypeProperty();
                etp.EntityTypeId = entityType.Id;
                etp.Name = column.ColumnName;
                
                byte i = 0;
                switch (column.DataType.ToString().ToLowerInvariant())
                {
                    case "system.boolean":
                    case "system.int16":
                    case "system.int32":
                    case "system.byte":
                        i = 1;
                        break;
                    case "system.datetime":
                        i = 2;
                        break;
                    case "system.double":
                        i = 3;
                        break;
                    case "system.int64":
                        i = 4;
                        break;
                    default:
                        i = 0;
                        break; 
                }
                Logging.LogInformation(string.Format("Column {0} is of type {1} mapped to id {2} ({3})",
                    column.ColumnName, column.DataType.ToString(), i.ToString(), row[column.Ordinal].ToString()));
                //Console.WriteLine("Column {0} is of type {1} mapped to id {2} ({3})",
                //    column.ColumnName, column.DataType.ToString(), i.ToString(), row[column.Ordinal].ToString());
                etp.TypeValue = i;
                etpList.Add(etp);
            }
            return etpList;
        }
    }
}
